package com.mustachio.urlshortener
{
	import com.mustachio.urlshortener.events.LinkBoundCalculationEvent;
	
	import flash.display.Sprite;
	import flash.text.TextField;
	
	import mx.controls.TextArea;
	
	public class CalcLinkBounds extends Sprite
	{
		
		private var count:uint;
		private var textWidth:uint;
		private var firstXPosition:uint;
		private var firstYPosition:uint;
		private var localTextArea:TextArea;
		private var textField:TextField;
		private var textHeight:uint;
		private var localLinkVO:LinkVO;
		
		public function CalcLinkBounds(incomingTextArea:TextArea, incomingStartingIndex:uint, incomingLinkVO:LinkVO)
		{
			//set the count to the starting index
			count = incomingStartingIndex;
			//set the width of the selected text to 0
			textWidth = 0;
			//set the localTextArea to the same value of the incomingTextArea so we can use the 
			//local one to iterate over its children and find its textField.
			localTextArea = incomingTextArea;
			//take the incoming linkVO and assign it to the local variables that we can
			//use to fill in the linkXPosition, linkYPosition, and textWidth properties.
			localLinkVO = incomingLinkVO;
			//this function down find the textField child in the TextArea component
			//and assigns it to a local global variable.
			findTextField();
			
		}
		
		public function runCalculation():void{
			//now run a loop over the 19 characters that are in the short url to
			//get a rectangle back with x,y,w,h properties.
			if(textField != null){
				for(var u:Number = 0; u <= 19; u++){
					if(textField.getCharBoundaries(count) != null){
						if(u == 0){
							firstXPosition = textField.getCharBoundaries(count).x;
							firstYPosition = textField.getCharBoundaries(count).y;
							localLinkVO.linkXPosition = firstXPosition;
							localLinkVO.linkYPosition = firstYPosition;
							textHeight = 12;
						}
						//trace(textField.getCharBoundaries(count));
						
						textWidth += textField.getCharBoundaries(count).width;
						count++;
						
						if(u == 19){
							localLinkVO.textLength = textWidth;
							var evt:LinkBoundCalculationEvent = new LinkBoundCalculationEvent(LinkBoundCalculationEvent.LINK_BOUND_CALCULATION_EVENT, false, false);
							evt.linkVO = localLinkVO;
							dispatchEvent(evt);
						}
					}
				}
				
			}
		}
		
		
		private function findTextField():void{
			//iterate over the children of the TextArea and find the UITextField child.
			//this may look kinda hackish but its the only way to access the UITextField
			//because it is protected inside the TextArea component and technically were not
			//supposed to have access to it.
			
			//we use the UITextField because of the method it has called getCharBoundaries
			//is how we are going to find out the exact position of each link in the TextArea
			
			//this for loop just traces out the name of the children so I know how to 
			//manually assign it to the local global variable textField.
			for(var i:Number = 0; i < localTextArea.numChildren; i++){
					trace(localTextArea.getChildAt(i));
				}
				
			//now that you can see the name print out in the trace window we assign it. 
			textField = localTextArea.getChildByName("UITextField7") as TextField;
		}
		
		public function get localTextField():TextField{
			return textField;
		}

	}
}